package com.komodo.island.gdx.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.komodo.island.gdx.Constants;
import com.komodo.island.gdx.Input;
import com.komodo.island.gdx.KomodoIsland;
import com.komodo.island.gdx.Persistence;
import com.komodo.island.gdx.dialog.Dialog;
import com.komodo.island.gdx.dialog.ExitDialog;
import com.komodo.island.gdx.dialog.MainMenuDialog;
import com.komodo.island.gdx.game.kodomosuit.SuitKomodo;
import com.komodo.island.gdx.game.komodobalance.GameBalance;
import com.komodo.island.gdx.game.komodomove.KomodoMove;
import com.komodo.island.gdx.game.komodophoto.KomodoPhoto;
import com.komodo.island.gdx.game.sandcastle.SandCastle;
import com.komodo.island.gdx.game.underwater.KomodoUnderwater;

public class MainMenuScreen extends Screen {

	public final static int EXIT = 3;

	// private TextureRegion background ;
	protected Dialog exitDialog;
	public MainMenuDialog mainMenuDialog;

	public MainMenuScreen() {
		// TODO Auto-generated constructor stub
		ID = Input.SCREEN_MAIN;
	}

	@Override
	public void init(KomodoIsland komodoIsland, Texture texture) {
		// TODO Auto-generated method stub
		super.init(komodoIsland, texture);
		// background = new
		// TextureRegion(texture,1,1,Constants.GAME_WIDTH,Constants.GAME_HEIGHT);

		// mainMenuDialog.setBackground(0, 0, Constants.GAME_WIDTH,
		// Constants.GAME_HEIGHT);

	}

	@Override
	public void render(float arg0) {
		// TODO Auto-generated method stub
		super.render(arg0);
		if (screenState == EXIT) {
			exitDialog.draw();

		}
	}

	@Override
	public void onScreenRun() {
		// TODO Auto-generated method stub
		if (komodoIsland.input.dialogShow) {
			mainMenuDialog.draw();
		}

	}

	@Override
	public void onScreenPause() {
		// TODO Auto-generated method stub
		pauseDialog.draw();
	}

	@Override
	public void onScreenLoading() {
		// TODO Auto-generated method stub
		loadingDialog.draw();
		// for (int i = 1 ; i<= 1000;i++){
		// System.out.println(i);
		// }

		// komodoIsland.additionTexture = new Texture(
		// Gdx.files.internal("addition1.png"));

		// LOADING
		/*
		 * ===================================================
		 */

		if (komodoIsland.buttonTexture == null)
			komodoIsland.buttonTexture = new Texture(
					Gdx.files.internal("button1.png"));
		// if (komodoIsland.gamedescTexture == null)
		// komodoIsland.gamedescTexture = new Texture(
		// Gdx.files.internal("story1.png"));
		if (komodoIsland.factTexture == null)
			komodoIsland.factTexture = new Texture(
					Gdx.files.internal("facts1.png"));
		if (komodoIsland.helpTexture == null)
			komodoIsland.helpTexture = new Texture(
					Gdx.files.internal("help1.png"));
		// komodoIsland.puzzleTexture = new Texture(
		// Gdx.files.internal("puzzle1.png"));

		if (komodoIsland.puzzlePieces == null) {
			komodoIsland.puzzlePieces = new TextureRegion[96];
			// komodoIsland.atlas = new
			// TextureAtlas(Gdx.files.internal("pack"));

			int x = 0, y = 0;
			for (int i = 0; i < 96; i++) {
				// if (y != 8) {
				komodoIsland.puzzlePieces[i] = new TextureRegion(
						komodoIsland.additionTexture, x * 55, y * 55, 55, 55);
				// } else {
				// komodoIsland.puzzlePieces[i] = new TextureRegion(
				// komodoIsland.puzzleTexture, x * 50, y * 50, 50, 50);
				// }
				if (x == 11) {
					x = 0;
					y++;
				} else {
					x++;
				}

			}
		}
		// System.out.println("Finish");
		//
		if (komodoIsland.startupAnimation == null) {
			komodoIsland.startupAnimation = new TextureRegion[3];
			komodoIsland.startupAnimation[0] = new TextureRegion(
					komodoIsland.menuTexture, 0, 320, 270, 270);
			komodoIsland.startupAnimation[1] = new TextureRegion(
					komodoIsland.menuTexture, 270, 320, 270, 270);
			komodoIsland.startupAnimation[2] = new TextureRegion(
					komodoIsland.menuTexture, 270 * 2, 320, 270, 270);
		}

		if (komodoIsland.playerScreen == null) {
			komodoIsland.playerScreen = new PlayerSelectionScreen();
			komodoIsland.playerScreen.init(komodoIsland,
					komodoIsland.menuTexture);
		}

		if (komodoIsland.helpScreen == null) {
			komodoIsland.helpScreen = new HelpScreen(null, 1, 5000, null);
			komodoIsland.helpScreen
					.init(komodoIsland, komodoIsland.helpTexture);
		}

		if (komodoIsland.scientistScreen == null) {
			komodoIsland.scientistScreen = new MapScientistScreen();
			komodoIsland.scientistScreen.init(komodoIsland,
					komodoIsland.menuTexture);
		}

		if (komodoIsland.touristScreen == null) {
			komodoIsland.touristScreen = new MapTouristScreen();
			komodoIsland.touristScreen.init(komodoIsland,
					komodoIsland.menuTexture);
		}

		if (komodoIsland.puzzleScreen == null) {
			komodoIsland.puzzleScreen = new PuzzleScreen();
			komodoIsland.puzzleScreen.init(komodoIsland,
					komodoIsland.menuTexture);
		}

		TextureRegion creditRegions[] = {
				new TextureRegion(komodoIsland.additionTexture, 660, 640, 480,
						320),
				new TextureRegion(komodoIsland.additionTexture, 660, 320, 480,
						320),
//				new TextureRegion(komodoIsland.additionTexture, 0, 320, 480,
//						320),

		 new TextureRegion(komodoIsland.additionTexture, 660, 0, 480,
		 320) // nanti diganti
		};

		if (komodoIsland.creditScreen == null) {
			komodoIsland.creditScreen = new CreditScreen(creditRegions, 3,
					komodoIsland.mainMenuScreen);
			komodoIsland.creditScreen.init(komodoIsland,
					komodoIsland.menuTexture);
		}

		if (komodoIsland.gameSuit == null) {
			komodoIsland.gameSuit = new SuitKomodo();
			komodoIsland.gameSuit.init(komodoIsland, komodoIsland.menuTexture);
		}

		if (komodoIsland.gameKomodoPhoto == null) {
			komodoIsland.gameKomodoPhoto = new KomodoPhoto();
			komodoIsland.gameKomodoPhoto.init(komodoIsland,
					komodoIsland.menuTexture);
		}

		if (komodoIsland.gameBalance == null) {
			komodoIsland.gameBalance = new GameBalance();
			komodoIsland.gameBalance.init(komodoIsland,
					komodoIsland.menuTexture);
		}

		// komodoIsland.gameShopping = new ShoppingGame();
		// komodoIsland.gameShopping.init(komodoIsland,
		// komodoIsland.menuTexture);

		if (komodoIsland.gameUnderWater == null) {
			komodoIsland.gameUnderWater = new KomodoUnderwater();
			komodoIsland.gameUnderWater.init(komodoIsland,
					komodoIsland.menuTexture);
		}

		if (komodoIsland.gameSandCastle == null) {
			komodoIsland.gameSandCastle = new SandCastle();
			komodoIsland.gameSandCastle.init(komodoIsland,
					komodoIsland.menuTexture);
		}

		if (komodoIsland.gameKomodoMove == null) {
			komodoIsland.gameKomodoMove = new KomodoMove();
			komodoIsland.gameKomodoMove.init(komodoIsland,
					komodoIsland.menuTexture);
		}

		TextureRegion[] scientistStory = { new TextureRegion(
				komodoIsland.menuTexture, 480 * 3, 0, 480, 320) };
		TextureRegion[] touristStory = { new TextureRegion(
				komodoIsland.menuTexture, 0, 320 + 270, 480, 320) };

		if (komodoIsland.storyScientist == null) {
			komodoIsland.storyScientist = new StoryScreen(scientistStory, 1,
					3000, komodoIsland.scientistScreen);
			komodoIsland.storyScientist.init(komodoIsland,
					komodoIsland.menuTexture);
		}
		if (komodoIsland.storyTourist == null) {
			komodoIsland.storyTourist = new StoryScreen(touristStory, 1, 3000,
					komodoIsland.touristScreen);
			komodoIsland.storyTourist.init(komodoIsland,
					komodoIsland.menuTexture);
		}

		//

		if (exitDialog == null)
			exitDialog = new ExitDialog(Constants.GAME_WIDTH,
					Constants.GAME_HEIGHT, true, this,
					komodoIsland.dialogTexture, komodoIsland);
		if (mainMenuDialog == null)
			mainMenuDialog = new MainMenuDialog(Constants.GAME_WIDTH,
					Constants.GAME_HEIGHT, true, this,
					komodoIsland.menuTexture, komodoIsland);

	}

	@Override
	public void exitScreen() {
		// TODO Auto-generated method stub
		super.exitScreen();
		if (previousScreen == null) {
			setScreenToExit();
		}
	}

	public void setScreenToExit() {
		setCameraToDefault();
		screenState = EXIT;
		komodoIsland.input.setStage(exitDialog);
	}

	@Override
	public void setScreenToRun() {
		// TODO Auto-generated method stub
		setCameraToDefault();
		screenState = RUNNING;
		if (Persistence.sound) {
			mainMenuDialog.removeActor(mainMenuDialog.soundButtonOff);
			mainMenuDialog.addActor(mainMenuDialog.soundButton);
		} else {
			mainMenuDialog.removeActor(mainMenuDialog.soundButton);
			mainMenuDialog.addActor(mainMenuDialog.soundButtonOff);
		}
		komodoIsland.input.setStage(mainMenuDialog);
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();

		if (Persistence.sound) {
			if (komodoIsland.main.isPlaying()) {

			} else
				komodoIsland.main.play();
		} else {
			komodoIsland.main.stop();
		}
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
		if (!Persistence.sound) {
			komodoIsland.main.stop();
		}
		// mainMenuDialog.clear();
	}

	@Override
	public void cleanResScreen() {
		// TODO Auto-generated method stub
		super.cleanResScreen();
		try {
			mainMenuDialog.dispose();
			exitDialog.dispose();
		} catch (Exception e) {

		}
	}
}
